﻿Imports System.Transactions
Imports log4net
Public Class AdvanceTask
    Dim logger As log4net.ILog = log4net.LogManager.GetLogger("AdvanceTask")
    Public Sub AdvanceCopy(ByVal _jobListRow As Job.JobListRow)
        Dim user As String = "BATCH"
        Dim vertionS As String = _jobListRow("FromVersion").ToString()
        Dim vertionT As String = _jobListRow("ToVersion").ToString()
        Dim planYearS As String = _jobListRow("PlanYear").ToString()
        Dim planYearT As String = _jobListRow("ToPlanYear").ToString()
        Dim chk1 As Boolean = If(_jobListRow("C1").ToString() = "True", True, False)
        Dim chk2 As Boolean = If(_jobListRow("C2").ToString() = "True", True, False)
        Dim chk3 As Boolean = If(_jobListRow("C3").ToString() = "True", True, False)
        Dim chk4 As Boolean = If(_jobListRow("C4").ToString() = "True", True, False)
        Dim chk5 As Boolean = If(_jobListRow("C5").ToString() = "True", True, False)
        If vertionT = "1" Or vertionT = "2" Or vertionT = "3" Or vertionT = "4" Or vertionT = "5" Then
            Throw New Exception("ไม่สามารถ Copy Version ไปยัง 1-5 ได้")
            Exit Sub
        End If
        If chk4 = True And chk5 = True Then
            Throw New Exception("ข้อ 4,5 ใช้ร่วมกันไม่ได้")
            Exit Sub
        End If
        Using scope As New TransactionScope()
            Try
                Dim sql As String = ""
                Dim adap As New copyverAdvanceDSTableAdapters.BudgetDetailsTableAdapter
                Dim adapb As New copyverAdvanceDSTableAdapters.Budget12TableAdapter
                Dim atadap As New copyverAdvanceDSTableAdapters.AttachmentsTableAdapter
                Dim dt As New copyverAdvanceDS.BudgetDetailsDataTable
                Dim dtb As New copyverAdvanceDS.Budget12DataTable
                If chk4 = True Then
                    sql = " select sum(Element1) as Element1 ,sum(Element2) as Element2 ,sum(Element3) as Element3 ,sum(Element4) as Element4 ,sum(Element5) as Element5 ,sum(Element6) as Element6 ,sum(Element7) as Element7 ,sum(Element8) as Element8 ,sum(Element9) as Element9 ,"
                    sql += " sum(Amount) as Amount,sum(NewQuantity) as NewQuantity,sum(ReplaceQuantity) as ReplaceQuantity, sum(Actual1) as Actual1, sum(Actual2) as Actual2, sum(Actual3) as Actual3, sum(Actual4) as Actual4,"
                    sql += " sum(Actual5) as Actual5, sum(Actual6) as Actual6, sum(Actual7) as Actual7, sum(Actual8) as Actual8, sum(Actual9) as Actual9, sum(Actual10) as Actual10,"
                    sql += " sum(Actual11) as Actual11, sum(Actual12) as Actual12 , sum(Actual13) as Actual13, sum(Actual14) as Actual14, sum(Actual15) as Actual15, "
                    sql += " sum(Actual16)  as Actual16, FundCenter_FundCenterCode , Cmmt_CmmtCode ,  FunctionalArea,Product_ProductCode, ActivityType_ActivityTypeCode,AssetNo"
                    sql += " FROM BudgetDetails where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                    sql += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode, AssetNo "
                    adap.GetDataByFilter(dt, sql)
                    If dt.Count > 0 Then
                        Dim amount As Decimal
                        Dim funcenter As String = ""
                        adap.DeleteByPlanVersion(planYearT, vertionT)
                        logger.Info("Delete [BudgetDetails, Budget12] planyear : " & planYearT & " version : " & vertionT)

                        atadap.DeleteByYearVersion(planYearT, vertionT)
                        logger.Info("Delete [Attachments] planyear : " & planYearT & " version : " & vertionT)

                        For Each row As copyverAdvanceDS.BudgetDetailsRow In dt
                            If chk1 = True Then
                                Dim adapP As New copyverAdvanceDSTableAdapters.fundpathTableAdapter
                                Dim group As String = ""
                                group = adapP.getGroupP(planYearS, row.FundCenter_FundCenterCode)
                                funcenter = getFund(group, planYearS)
                            Else
                                funcenter = row.FundCenter_FundCenterCode
                            End If
                            If chk3 = True Then
                                amount = If(row.IsActual1Null, 0, row.Actual1) + If(row.IsActual2Null, 0, row.Actual2) + If(row.IsActual3Null, 0, row.Actual3) + If(row.IsActual4Null, 0, row.Actual4) + If(row.IsActual5Null, 0, row.Actual5) + If(row.IsActual6Null, 0, row.Actual6) + If(row.IsActual7Null, 0, row.Actual7) + If(row.IsActual8Null, 0, row.Actual8) + If(row.IsActual9Null, 0, row.Actual9) + If(row.IsActual10Null, 0, row.Actual10) + If(row.IsActual11Null, 0, row.Actual11) + If(row.IsActual12Null, 0, row.Actual12)
                            Else
                                amount = If(row.IsAmountNull, 0, row.Amount)
                            End If
                            Dim newId As Integer = adap.InsertBudgetCopy(planYearT, planYearT, vertionT, If(row.IsAssetNoNull, Nothing, row.AssetNo), Nothing, Nothing, Nothing, If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsFunctionalAreaNull, Nothing, row.FunctionalArea), If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), Nothing, Nothing, Date.Now, Nothing, user, Nothing, planYearT, Nothing, Nothing, Nothing, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), planYearT, funcenter, planYearT, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), planYearT)
                            Dim sqlb As String = ""
                            sqlb = "  SELECT  sum(Amount1) as Amount1,sum(Amount2) as Amount2,sum(Amount3) as Amount3,sum(Amount4) as Amount4,sum(Amount5) as Amount5"
                            sqlb += " ,sum(Amount6) as Amount6,sum(Amount7) as Amount7,sum(Amount8) as Amount8,sum(Amount9) as Amount9,sum(Amount10) as Amount10"
                            sqlb += " ,sum(Amount11) as Amount11,sum(Amount12) as Amount12,sum(ForecastYear1) as ForecastYear1,"
                            sqlb += " sum(ForecastYear2) as ForecastYear2,sum(ForecastYear3) as ForecastYear3,sum(ForecastYear4) as ForecastYear4"
                            sqlb += " ,FundCenter_FundCenterCode,Cmmt_CmmtCode,FunctionalArea"
                            sqlb += " ,Product_ProductCode,ActivityType_ActivityTypeCode"
                            sqlb += "  FROM Budget12  where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                            sqlb += " and FundCenter_FundCenterCode = '" & funcenter & "'"
                            sqlb += " and " & If(row.IsCmmt_CmmtCodeNull, " Cmmt_CmmtCode IS NULL ", " Cmmt_CmmtCode  = '" & row.Cmmt_CmmtCode & "'")
                            sqlb += " and " & If(row.IsFunctionalAreaNull, " FunctionalArea is null ", " FunctionalArea = '" & row.FunctionalArea & "'")
                            sqlb += " and " & If(row.IsProduct_ProductCodeNull, " Product_ProductCode is null ", " Product_ProductCode = '" & row.Product_ProductCode & "'")
                            sqlb += " and " & If(row.IsActivityType_ActivityTypeCodeNull, " ActivityType_ActivityTypeCode is null ", " ActivityType_ActivityTypeCode = '" & row.ActivityType_ActivityTypeCode & "'")
                            sqlb += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode "
                            adapb.GetDatagroup(dtb, sqlb)
                            For Each rowb As copyverAdvanceDS.Budget12Row In dtb
                                Dim adb As New copyverAdvanceDSTableAdapters.Budget12TableAdapter
                                adb.InsertBudget12Copy(planYearT, planYearT, vertionT, If(rowb.IsFunctionalAreaNull, Nothing, rowb.FunctionalArea), Nothing, rowb.Amount1, rowb.Amount2, rowb.Amount3, rowb.Amount4, rowb.Amount5, rowb.Amount6, rowb.Amount7, rowb.Amount8, rowb.Amount9, rowb.Amount10, rowb.Amount11, rowb.Amount12, rowb.ForecastYear1, rowb.ForecastYear2, rowb.ForecastYear3, rowb.ForecastYear4, Nothing, Date.Now, Nothing, user, Nothing, rowb.FundCenter_FundCenterCode, planYearT, Nothing, Nothing, If(rowb.IsCmmt_CmmtCodeNull, Nothing, rowb.Cmmt_CmmtCode), planYearT, Nothing, Nothing, If(rowb.IsActivityType_ActivityTypeCodeNull, Nothing, rowb.ActivityType_ActivityTypeCode), Nothing, Nothing, If(rowb.IsProduct_ProductCodeNull, Nothing, rowb.Product_ProductCode), planYearT, Nothing, Nothing, Nothing, newId)
                            Next
                        Next
                    End If
                ElseIf chk2 = True And chk5 = True Then
                    sql = " select sum(Element1) as Element1 ,sum(Element2) as Element2 ,sum(Element3) as Element3 ,sum(Element4) as Element4 ,sum(Element5) as Element5 ,sum(Element6) as Element6 ,sum(Element7) as Element7 ,sum(Element8) as Element8 ,sum(Element9) as Element9 ,"
                    sql += " sum(Amount) as Amount,sum(NewQuantity) as NewQuantity,sum(ReplaceQuantity) as ReplaceQuantity, sum(Actual1) as Actual1, sum(Actual2) as Actual2, sum(Actual3) as Actual3, sum(Actual4) as Actual4,"
                    sql += " sum(Actual5) as Actual5, sum(Actual6) as Actual6, sum(Actual7) as Actual7, sum(Actual8) as Actual8, sum(Actual9) as Actual9, sum(Actual10) as Actual10,"
                    sql += " sum(Actual11) as Actual11, sum(Actual12) as Actual12 , sum(Actual13) as Actual13, sum(Actual14) as Actual14, sum(Actual15) as Actual15, "
                    sql += " sum(Actual16) as Actual16 ,  FundCenter_FundCenterCode , FunctionalArea,Cmmt_CmmtCode , AssetNo,"
                    sql += "  ActivityType_ActivityTypeCode, Product_ProductCode,FP_CODE"
                    sql += " from (select Element1,Element2,Element3,Element4,Element5,Element6,Element7,Element8,Element9,Amount,NewQuantity,ReplaceQuantity,  Actual1,  Actual2,  Actual3, Actual4,"
                    sql += " Actual5,  Actual6,  Actual7,  Actual8, Actual9,  Actual10,"
                    sql += " Actual11,  Actual12 , Actual13,  Actual14, Actual15, "
                    sql += "  Actual16 , FundCenter_FundCenterCode , FunctionalArea ,  Cmmt_CmmtCode , AssetNo, "
                    sql += "  ActivityType_ActivityTypeCode, Product_ProductCode,"
                    sql += "   case when ISNUMERIC(SUBSTRING(isnull(FundedProgram,''), 1, 1)) = 1 then 'A' when SUBSTRING(isnull(FundedProgram,''), 1, 1)='' "
                    sql += " then '' else SUBSTRING(isnull(FundedProgram,''), 1, 1) end AS FP_CODE FROM BudgetDetails "
                    sql += " where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')) as x"
                    sql += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode, AssetNo , FP_CODE"
                    adap.GetDataByFilter(dt, sql)
                    If dt.Count > 0 Then
                        Dim amount As Decimal
                        Dim funcenter As String
                        adap.DeleteByPlanVersion(planYearT, vertionT)
                        logger.Info("Delete [BudgetDetails, Budget12] planyear : " & planYearT & " version : " & vertionT)

                        atadap.DeleteByYearVersion(planYearT, vertionT)
                        logger.Info("Delete [Attachments] planyear : " & planYearT & " version : " & vertionT)

                        For Each row As copyverAdvanceDS.BudgetDetailsRow In dt
                            If chk1 = True Then
                                Dim adapP As New copyverAdvanceDSTableAdapters.fundpathTableAdapter
                                Dim group As String = ""
                                group = adapP.getGroupP(planYearS, row.FundCenter_FundCenterCode)
                                funcenter = getFund(group, planYearS)
                            Else
                                funcenter = row.FundCenter_FundCenterCode
                            End If
                            If chk3 = True Then
                                amount = If(row.IsActual1Null, 0, row.Actual1) + If(row.IsActual2Null, 0, row.Actual2) + If(row.IsActual3Null, 0, row.Actual3) + If(row.IsActual4Null, 0, row.Actual4) + If(row.IsActual5Null, 0, row.Actual5) + If(row.IsActual6Null, 0, row.Actual6) + If(row.IsActual7Null, 0, row.Actual7) + If(row.IsActual8Null, 0, row.Actual8) + If(row.IsActual9Null, 0, row.Actual9) + If(row.IsActual10Null, 0, row.Actual10) + If(row.IsActual11Null, 0, row.Actual11) + If(row.IsActual12Null, 0, row.Actual12)
                            Else
                                amount = If(row.IsAmountNull, 0, row.Amount)
                            End If
                            Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
                            Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                            Dim newId As Integer = adap.InsertBudgetCopy(planYearT, planYearT, vertionT, If(row.IsAssetNoNull, Nothing, row.AssetNo), Nothing, Nothing, Nothing, If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsFunctionalAreaNull, Nothing, row.FunctionalArea), If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), Nothing, Nothing, Date.Now, Nothing, user, Nothing, planYearT, If(row.IsActivityType_ActivityTypeCodeNull, Nothing, row.ActivityType_ActivityTypeCode), Nothing, Nothing, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), planYearT, funcenter, planYearT, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), planYearT)
                            Dim sqlb As String = ""
                            sqlb = " select   sum(Amount1) as Amount1,sum(Amount2) as Amount2,sum(Amount3) as Amount3,sum(Amount4) as Amount4,sum(Amount5) as Amount5"
                            sqlb += " ,sum(Amount6) as Amount6,sum(Amount7) as Amount7,sum(Amount8) as Amount8,sum(Amount9) as Amount9,sum(Amount10) as Amount10"
                            sqlb += " ,sum(Amount11) as Amount11,sum(Amount12) as Amount12,sum(ForecastYear1) as ForecastYear1,"
                            sqlb += " sum(ForecastYear2) as ForecastYear2,sum(ForecastYear3) as ForecastYear3,sum(ForecastYear4) as ForecastYear4"
                            sqlb += " ,FundCenter_FundCenterCode,Cmmt_CmmtCode,FunctionalArea"
                            sqlb += " ,Product_ProductCode,ActivityType_ActivityTypeCode, FP_CODE "
                            sqlb += "  from ( SELECT  Amount1, Amount2, Amount3, Amount4, Amount5"
                            sqlb += " , Amount6, Amount7, Amount8, Amount9, Amount10"
                            sqlb += " , Amount11, Amount12, ForecastYear1,"
                            sqlb += "  ForecastYear2, ForecastYear3, ForecastYear4"
                            sqlb += " ,FundCenter_FundCenterCode,Cmmt_CmmtCode,FunctionalArea"
                            sqlb += " ,Product_ProductCode,ActivityType_ActivityTypeCode,"
                            sqlb += "   case when ISNUMERIC(SUBSTRING(isnull(FundedProgram,''), 1, 1)) = 1 then 'A' when SUBSTRING(isnull(FundedProgram,''), 1, 1)='' "
                            sqlb += " then '' else SUBSTRING(isnull(FundedProgram,''), 1, 1) end AS FP_CODE "
                            sqlb += "  FROM Budget12  where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                            sqlb += " and FundCenter_FundCenterCode = '" & funcenter & "'"
                            sqlb += " and " & If(row.IsCmmt_CmmtCodeNull, " Cmmt_CmmtCode IS NULL ", " Cmmt_CmmtCode  = '" & row.Cmmt_CmmtCode & "'")
                            sqlb += " and " & If(row.IsFunctionalAreaNull, " FunctionalArea is null ", " FunctionalArea = '" & row.FunctionalArea & "'")
                            sqlb += " and " & If(row.IsProduct_ProductCodeNull, " Product_ProductCode is null ", " Product_ProductCode = '" & row.Product_ProductCode & "'")
                            sqlb += " and " & If(row.IsActivityType_ActivityTypeCodeNull, " ActivityType_ActivityTypeCode is null ", " ActivityType_ActivityTypeCode = '" & row.ActivityType_ActivityTypeCode & "'")
                            sqlb += " ) as x "
                            sqlb += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode , FP_CODE "
                            adapb.GetDatagroup(dtb, sqlb)
                            For Each rowb As copyverAdvanceDS.Budget12Row In dtb
                                Dim adb As New copyverAdvanceDSTableAdapters.Budget12TableAdapter
                                adb.InsertBudget12Copy(planYearT, planYearT, vertionT, If(rowb.IsFunctionalAreaNull, Nothing, rowb.FunctionalArea), Nothing, rowb.Amount1, rowb.Amount2, rowb.Amount3, rowb.Amount4, rowb.Amount5, rowb.Amount6, rowb.Amount7, rowb.Amount8, rowb.Amount9, rowb.Amount10, rowb.Amount11, rowb.Amount12, rowb.ForecastYear1, rowb.ForecastYear2, rowb.ForecastYear3, rowb.ForecastYear4, Nothing, Date.Now, Nothing, user, Nothing, rowb.FundCenter_FundCenterCode, planYearT, Nothing, Nothing, If(rowb.IsCmmt_CmmtCodeNull, Nothing, rowb.Cmmt_CmmtCode), planYearT, Nothing, Nothing, If(rowb.IsActivityType_ActivityTypeCodeNull, Nothing, rowb.ActivityType_ActivityTypeCode), Nothing, Nothing, If(rowb.IsProduct_ProductCodeNull, Nothing, rowb.Product_ProductCode), planYearT, Nothing, Nothing, Nothing, newId)
                            Next
                        Next
                    End If
                ElseIf chk5 = True Then
                    sql = " select sum(Element1) as Element1 ,sum(Element2) as Element2 ,sum(Element3) as Element3 ,sum(Element4) as Element4 ,sum(Element5) as Element5 ,sum(Element6) as Element6 ,sum(Element7) as Element7 ,sum(Element8) as Element8 ,sum(Element9) as Element9 ,"
                    sql += " sum(Amount) as Amount,sum(NewQuantity) as NewQuantity,sum(ReplaceQuantity) as ReplaceQuantity, sum(Actual1) as Actual1, sum(Actual2) as Actual2, sum(Actual3) as Actual3, sum(Actual4) as Actual4,"
                    sql += " sum(Actual5) as Actual5, sum(Actual6) as Actual6, sum(Actual7) as Actual7, sum(Actual8) as Actual8, sum(Actual9) as Actual9, sum(Actual10) as Actual10,"
                    sql += " sum(Actual11) as Actual11, sum(Actual12) as Actual12 , sum(Actual13) as Actual13, sum(Actual14) as Actual14, sum(Actual15) as Actual15, "
                    sql += " sum(Actual16) as Actual16 ,  FundCenter_FundCenterCode ,  Cmmt_CmmtCode "
                    sql += " , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode"
                    sql += " ,  AssetNo, Project_ProjectCode, FundedProgram"
                    sql += " FROM BudgetDetails where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                    sql += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode, AssetNo, Project_ProjectCode, FundedProgram"
                    adap.GetDataByFilter(dt, sql)
                    If dt.Count > 0 Then
                        Dim amount As Decimal
                        Dim funcenter As String
                        adap.DeleteByPlanVersion(planYearT, vertionT)
                        logger.Info("Delete [BudgetDetails, Budget12] planyear : " & planYearT & " version : " & vertionT)
                        atadap.DeleteByYearVersion(planYearT, vertionT)
                        logger.Info("Delete [Attachments] planyear : " & planYearT & " version : " & vertionT)

                        For Each row As copyverAdvanceDS.BudgetDetailsRow In dt
                            If chk1 = True Then
                                Dim adapP As New copyverAdvanceDSTableAdapters.fundpathTableAdapter
                                Dim group As String = ""
                                group = adapP.getGroupP(planYearS, row.FundCenter_FundCenterCode)
                                funcenter = getFund(group, planYearS)
                            Else
                                funcenter = row.FundCenter_FundCenterCode
                            End If
                            If chk3 = True Then
                                amount = If(row.IsActual1Null, 0, row.Actual1) + If(row.IsActual2Null, 0, row.Actual2) + If(row.IsActual3Null, 0, row.Actual3) + If(row.IsActual4Null, 0, row.Actual4) + If(row.IsActual5Null, 0, row.Actual5) + If(row.IsActual6Null, 0, row.Actual6) + If(row.IsActual7Null, 0, row.Actual7) + If(row.IsActual8Null, 0, row.Actual8) + If(row.IsActual9Null, 0, row.Actual9) + If(row.IsActual10Null, 0, row.Actual10) + If(row.IsActual11Null, 0, row.Actual11) + If(row.IsActual12Null, 0, row.Actual12)
                            Else
                                amount = If(row.IsAmountNull, 0, row.Amount)
                            End If
                            Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
                            Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                            Dim newId As Integer = adap.InsertBudgetCopy(planYearT, planYearT, vertionT, If(row.IsAssetNoNull, Nothing, row.AssetNo), Nothing, Nothing, Nothing, If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsFundedProgramNull, Nothing, row.FundedProgram), Nothing, If(row.IsFunctionalAreaNull, Nothing, row.FunctionalArea), If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), Nothing, Nothing, Date.Now, Nothing, user, Nothing, planYearT, If(row.IsActivityType_ActivityTypeCodeNull, Nothing, row.ActivityType_ActivityTypeCode), Nothing, Nothing, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), planYearT, funcenter, planYearT, If(row.IsProject_ProjectCodeNull, Nothing, row.Project_ProjectCode), planYearT, Nothing, Nothing, Nothing, Nothing, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), planYearT)
                            Dim sqlb As String = ""
                            sqlb = "  SELECT  sum(Amount1) as Amount1,sum(Amount2) as Amount2,sum(Amount3) as Amount3,sum(Amount4) as Amount4,sum(Amount5) as Amount5"
                            sqlb += " ,sum(Amount6) as Amount6,sum(Amount7) as Amount7,sum(Amount8) as Amount8,sum(Amount9) as Amount9,sum(Amount10) as Amount10"
                            sqlb += " ,sum(Amount11) as Amount11,sum(Amount12) as Amount12,sum(ForecastYear1) as ForecastYear1,"
                            sqlb += " sum(ForecastYear2) as ForecastYear2,sum(ForecastYear3) as ForecastYear3,sum(ForecastYear4) as ForecastYear4"
                            sqlb += " ,FundCenter_FundCenterCode,Cmmt_CmmtCode,FunctionalArea"
                            sqlb += " ,Product_ProductCode,ActivityType_ActivityTypeCode, Project_ProjectCode, FundedProgram "
                            sqlb += "  FROM Budget12  where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                            sqlb += " and FundCenter_FundCenterCode = '" & funcenter & "'"
                            sqlb += " and " & If(row.IsCmmt_CmmtCodeNull, " Cmmt_CmmtCode IS NULL ", " Cmmt_CmmtCode  = '" & row.Cmmt_CmmtCode & "'")
                            sqlb += " and " & If(row.IsFunctionalAreaNull, " FunctionalArea is null ", " FunctionalArea = '" & row.FunctionalArea & "'")
                            sqlb += " and " & If(row.IsProduct_ProductCodeNull, " Product_ProductCode is null ", " Product_ProductCode = '" & row.Product_ProductCode & "'")
                            sqlb += " and " & If(row.IsActivityType_ActivityTypeCodeNull, " ActivityType_ActivityTypeCode is null ", " ActivityType_ActivityTypeCode = '" & row.ActivityType_ActivityTypeCode & "'")
                            sqlb += " and " & If(row.IsProject_ProjectCodeNull, " Project_ProjectCode is null ", " Project_ProjectCode = '" & row.Project_ProjectCode & "'")
                            sqlb += " and " & If(row.IsFundedProgramNull, " FundedProgram is null ", " FundedProgram = '" & row.FundedProgram & "'")
                            sqlb += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode , FunctionalArea,  Product_ProductCode, ActivityType_ActivityTypeCode, Project_ProjectCode, FundedProgram"
                            adapb.GetDatagroup(dtb, sqlb)
                            For Each rowb As copyverAdvanceDS.Budget12Row In dtb
                                Dim adb As New copyverAdvanceDSTableAdapters.Budget12TableAdapter
                                adb.InsertBudget12Copy(planYearT, planYearT, vertionT, If(rowb.IsFunctionalAreaNull, Nothing, rowb.FunctionalArea), If(rowb.IsFundedProgramNull, Nothing, rowb.FundedProgram), rowb.Amount1, rowb.Amount2, rowb.Amount3, rowb.Amount4, rowb.Amount5, rowb.Amount6, rowb.Amount7, rowb.Amount8, rowb.Amount9, rowb.Amount10, rowb.Amount11, rowb.Amount12, rowb.ForecastYear1, rowb.ForecastYear2, rowb.ForecastYear3, rowb.ForecastYear4, Nothing, Date.Now, Nothing, user, Nothing, rowb.FundCenter_FundCenterCode, planYearT, Nothing, Nothing, If(rowb.IsCmmt_CmmtCodeNull, Nothing, rowb.Cmmt_CmmtCode), planYearT, Nothing, Nothing, If(rowb.IsActivityType_ActivityTypeCodeNull, Nothing, rowb.ActivityType_ActivityTypeCode), Nothing, Nothing, If(rowb.IsProduct_ProductCodeNull, Nothing, rowb.Product_ProductCode), planYearT, If(rowb.IsProject_ProjectCodeNull, Nothing, rowb.Project_ProjectCode), Nothing, Nothing, newId)
                            Next
                        Next
                    End If
                ElseIf chk2 = True Then
                    sql = " select sum(Element1) as Element1 ,sum(Element2) as Element2 ,sum(Element3) as Element3 ,sum(Element4) as Element4 ,sum(Element5) as Element5 ,sum(Element6) as Element6 ,sum(Element7) as Element7 ,sum(Element8) as Element8 ,sum(Element9) as Element9 ,"
                    sql += " sum(Amount) as Amount,sum(NewQuantity) as NewQuantity,sum(ReplaceQuantity) as ReplaceQuantity, sum(Actual1) as Actual1, sum(Actual2) as Actual2, sum(Actual3) as Actual3, sum(Actual4) as Actual4,"
                    sql += " sum(Actual5) as Actual5, sum(Actual6) as Actual6, sum(Actual7) as Actual7, sum(Actual8) as Actual8, sum(Actual9) as Actual9, sum(Actual10) as Actual10,"
                    sql += " sum(Actual11) as Actual11, sum(Actual12) as Actual12 , sum(Actual13) as Actual13, sum(Actual14) as Actual14, sum(Actual15) as Actual15, "
                    sql += " sum(Actual16) as Actual16 , FundCenter_FundCenterCode , Cmmt_CmmtCode "
                    sql += " , CmmtDetail_Id, Activity_ActivityCode,"
                    sql += "  ActivityType_ActivityTypeCode,  Product_ProductCode,FP_CODE "
                    sql += " from (select Element1,Element2,Element3,Element4,Element5,Element6,Element7,Element8,Element9,Amount,NewQuantity,ReplaceQuantity,  Actual1,  Actual2,  Actual3, Actual4,"
                    sql += " Actual5,  Actual6,  Actual7,  Actual8, Actual9,  Actual10,"
                    sql += " Actual11,  Actual12 , Actual13,  Actual14, Actual15, "
                    sql += " Actual16 , FundCenter_FundCenterCode , Cmmt_CmmtCode "
                    sql += " , CmmtDetail_Id, Activity_ActivityCode,"
                    sql += "  ActivityType_ActivityTypeCode,  Product_ProductCode,"
                    sql += " case when ISNUMERIC(SUBSTRING(isnull(FundedProgram,''), 1, 1)) = 1 then 'A' when SUBSTRING(isnull(FundedProgram,''), 1, 1)='' "
                    sql += " then '' else SUBSTRING(isnull(FundedProgram,''), 1, 1) end AS FP_CODE FROM BudgetDetails "
                    sql += " where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')) as x"
                    sql += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode ,CmmtDetail_Id,Activity_ActivityCode, ActivityType_ActivityTypeCode, Product_ProductCode,FP_CODE"
                    adap.GetDataByFilter(dt, sql)
                    If dt.Count > 0 Then
                        Dim amount As Decimal
                        Dim funcenter As String
                        adap.DeleteByPlanVersion(planYearT, vertionT)
                        logger.Info("Delete [BudgetDetails, Budget12] planyear : " & planYearT & " version : " & vertionT)

                        atadap.DeleteByYearVersion(planYearT, vertionT)
                        logger.Info("Delete [Attachments] planyear : " & planYearT & " version : " & vertionT)

                        For Each row As copyverAdvanceDS.BudgetDetailsRow In dt
                            If chk1 = True Then
                                Dim adapP As New copyverAdvanceDSTableAdapters.fundpathTableAdapter
                                Dim group As String = ""
                                group = adapP.getGroupP(planYearS, row.FundCenter_FundCenterCode)
                                funcenter = getFund(group, planYearS)
                            Else
                                funcenter = row.FundCenter_FundCenterCode
                            End If
                            If chk3 = True Then
                                amount = If(row.IsActual1Null, 0, row.Actual1) + If(row.IsActual2Null, 0, row.Actual2) + If(row.IsActual3Null, 0, row.Actual3) + If(row.IsActual4Null, 0, row.Actual4) + If(row.IsActual5Null, 0, row.Actual5) + If(row.IsActual6Null, 0, row.Actual6) + If(row.IsActual7Null, 0, row.Actual7) + If(row.IsActual8Null, 0, row.Actual8) + If(row.IsActual9Null, 0, row.Actual9) + If(row.IsActual10Null, 0, row.Actual10) + If(row.IsActual11Null, 0, row.Actual11) + If(row.IsActual12Null, 0, row.Actual12)
                            Else
                                amount = If(row.IsAmountNull, 0, row.Amount)
                            End If
                            Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
                            Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                            Dim newId As Integer = 0
                            If row.IsCmmtDetail_IdNull Then
                                newId = adap.InsertBudgetCopy(planYearT, planYearT, vertionT, Nothing, Nothing, Nothing, Nothing, If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), Nothing, Nothing, Date.Now, Nothing, user, Nothing, planYearT, If(row.IsActivityType_ActivityTypeCodeNull, Nothing, row.ActivityType_ActivityTypeCode), If(row.IsActivity_ActivityCodeNull, Nothing, row.Activity_ActivityCode), planYearT, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), planYearT, funcenter, planYearT, Nothing, planYearT, Nothing, Nothing, Nothing, Nothing, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), planYearT)
                            Else
                                newId = adap.InsertBudgetCopy(planYearT, planYearT, vertionT, Nothing, Nothing, Nothing, Nothing, If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), Nothing, Nothing, Date.Now, Nothing, user, Nothing, planYearT, If(row.IsActivityType_ActivityTypeCodeNull, Nothing, row.ActivityType_ActivityTypeCode), If(row.IsActivity_ActivityCodeNull, Nothing, row.Activity_ActivityCode), planYearT, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), planYearT, funcenter, planYearT, Nothing, planYearT, Nothing, row.CmmtDetail_Id, Nothing, Nothing, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), planYearT)
                            End If
                            If newId <> 0 Then
                                Dim sqlb As String = ""
                                sqlb = " select   sum(Amount1) as Amount1,sum(Amount2) as Amount2,sum(Amount3) as Amount3,sum(Amount4) as Amount4,sum(Amount5) as Amount5"
                                sqlb += " ,sum(Amount6) as Amount6,sum(Amount7) as Amount7,sum(Amount8) as Amount8,sum(Amount9) as Amount9,sum(Amount10) as Amount10"
                                sqlb += " ,sum(Amount11) as Amount11,sum(Amount12) as Amount12,sum(ForecastYear1) as ForecastYear1,"
                                sqlb += " sum(ForecastYear2) as ForecastYear2,sum(ForecastYear3) as ForecastYear3,sum(ForecastYear4) as ForecastYear4"
                                sqlb += " ,FundCenter_FundCenterCode,Cmmt_CmmtCode,CmmtDetail_Id"
                                sqlb += " ,Product_ProductCode,Activity_ActivityCode,ActivityType_ActivityTypeCode, FP_CODE "
                                sqlb += "  from ( SELECT  Amount1, Amount2, Amount3, Amount4, Amount5"
                                sqlb += " , Amount6, Amount7, Amount8, Amount9, Amount10"
                                sqlb += " , Amount11, Amount12, ForecastYear1,"
                                sqlb += "  ForecastYear2, ForecastYear3, ForecastYear4"
                                sqlb += " ,FundCenter_FundCenterCode,Cmmt_CmmtCode,CmmtDetail_Id"
                                sqlb += " ,Product_ProductCode,Activity_ActivityCode,ActivityType_ActivityTypeCode,"
                                sqlb += "   case when ISNUMERIC(SUBSTRING(isnull(FundedProgram,''), 1, 1)) = 1 then 'A' when SUBSTRING(isnull(FundedProgram,''), 1, 1)='' "
                                sqlb += " then '' else SUBSTRING(isnull(FundedProgram,''), 1, 1) end AS FP_CODE "
                                sqlb += "  FROM Budget12  where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                                sqlb += " and FundCenter_FundCenterCode = '" & funcenter & "'"
                                sqlb += " and " & If(row.IsCmmt_CmmtCodeNull, " Cmmt_CmmtCode IS NULL ", " Cmmt_CmmtCode  = '" & row.Cmmt_CmmtCode & "'")
                                sqlb += " and " & If(row.IsFunctionalAreaNull, " FunctionalArea is null ", " FunctionalArea = '" & row.FunctionalArea & "'")
                                sqlb += " and " & If(row.IsProduct_ProductCodeNull, " Product_ProductCode is null ", " Product_ProductCode = '" & row.Product_ProductCode & "'")
                                sqlb += " and " & If(row.IsActivityType_ActivityTypeCodeNull, " ActivityType_ActivityTypeCode is null ", " ActivityType_ActivityTypeCode = '" & row.ActivityType_ActivityTypeCode & "'")
                                sqlb += " and " & If(row.IsActivity_ActivityCodeNull, " Activity_ActivityCode is null ", " Activity_ActivityCode = '" & row.Activity_ActivityCode & "'")
                                sqlb += " and " & If(row.IsCmmtDetail_IdNull, " CmmtDetail_Id is null ", " CmmtDetail_Id = '" & row.CmmtDetail_Id & "'")
                                sqlb += " ) as x "
                                sqlb += " Group By FundCenter_FundCenterCode , Cmmt_CmmtCode ,CmmtDetail_Id,Activity_ActivityCode, ActivityType_ActivityTypeCode, Product_ProductCode,FP_CODE "
                                adapb.GetDatagroup(dtb, sqlb)
                                For Each rowb As copyverAdvanceDS.Budget12Row In dtb
                                    Dim adb As New copyverAdvanceDSTableAdapters.Budget12TableAdapter
                                    adb.InsertBudget12Copy(planYearT, planYearT, vertionT, If(rowb.IsFunctionalAreaNull, Nothing, rowb.FunctionalArea), Nothing, rowb.Amount1, rowb.Amount2, rowb.Amount3, rowb.Amount4, rowb.Amount5, rowb.Amount6, rowb.Amount7, rowb.Amount8, rowb.Amount9, rowb.Amount10, rowb.Amount11, rowb.Amount12, rowb.ForecastYear1, rowb.ForecastYear2, rowb.ForecastYear3, rowb.ForecastYear4, Nothing, Date.Now, Nothing, user, Nothing, rowb.FundCenter_FundCenterCode, planYearT, Nothing, Nothing, If(rowb.IsCmmt_CmmtCodeNull, Nothing, rowb.Cmmt_CmmtCode), planYearT, Nothing, planYearT, If(rowb.IsActivityType_ActivityTypeCodeNull, Nothing, rowb.ActivityType_ActivityTypeCode), If(rowb.IsActivity_ActivityCodeNull, Nothing, rowb.Activity_ActivityCode), planYearT, If(rowb.IsProduct_ProductCodeNull, Nothing, rowb.Product_ProductCode), planYearT, Nothing, Nothing, Nothing, newId)
                                Next
                            End If
                        Next
                    End If
                Else
                    sql = " SELECT Id,PlanYear,BaseYear,Version,AssetNo,AssetDescription,AssetRowNo,OldAssetNo,Element1,Element2"
                    sql += " ,Element3,Element4,Element5,Element6,Element7,Element8,Element9,Amount,NewQuantity,ReplaceQuantity"
                    sql += " ,PricePerUnit,Note,Identification,FormId,FndPrgGenFlag,FundedProgram,TmpFundedProgram"
                    sql += " ,FunctionalArea,Actual1, Actual2, Actual3,Actual4,Actual5, Actual6, Actual7,Actual8, Actual9, Actual10, Actual11"
                    sql += " ,Actual12, Actual13, Actual14, Actual15, Actual16,Attachments_amount,ProductItemNo,CreatedDate,UpdatedDate"
                    sql += " ,CreatedBy,UpdatedBy,ActivityType_PlanYear,ActivityType_ActivityTypeCode,Activity_ActivityCode,Activity_PlanYear"
                    sql += " ,Product_ProductCode,Product_PlanYear,FundCenter_FundCenterCode,FundCenter_PlanYear,Project_ProjectCode"
                    sql += " ,Project_PlanYear,Project_FundCenterCode,CmmtDetail_Id,Fund_FundCode,Fund_PlanYear,Cmmt_CmmtCode,Cmmt_PlanYear"
                    sql += " FROM BudgetDetails where (PlanYear = " & planYearS & ") AND (Version = '" & vertionS & "')"
                    adap.GetDataByFilter(dt, sql)
                    If dt.Count > 0 Then
                        Dim amount As Decimal
                        Dim funcenter As String
                        adap.DeleteByPlanVersion(planYearT, vertionT)
                        logger.Info("Delete [BudgetDetails, Budget12] planyear : " & planYearT & " version : " & vertionT)

                        atadap.DeleteByYearVersion(planYearT, vertionT)
                        logger.Info("Delete [Attachments] planyear : " & planYearT & " version : " & vertionT)

                        Dim adapRev As New FundCenter2TableAdapters.BudgetRevPlansTableAdapter
                        adapRev.DeleteByVersion(planYearT, vertionT)
                        For Each row As copyverAdvanceDS.BudgetDetailsRow In dt
                            If chk1 = True Then
                                Dim adapP As New copyverAdvanceDSTableAdapters.fundpathTableAdapter
                                Dim group As String = ""
                                group = adapP.getGroupP(planYearS, row.FundCenter_FundCenterCode)
                                funcenter = getFund(group, planYearS)
                            Else
                                funcenter = row.FundCenter_FundCenterCode
                            End If
                            If chk3 = True Then
                                amount = If(row.IsActual1Null, 0, row.Actual1) + If(row.IsActual2Null, 0, row.Actual2) + If(row.IsActual3Null, 0, row.Actual3) + If(row.IsActual4Null, 0, row.Actual4) + If(row.IsActual5Null, 0, row.Actual5) + If(row.IsActual6Null, 0, row.Actual6) + If(row.IsActual7Null, 0, row.Actual7) + If(row.IsActual8Null, 0, row.Actual8) + If(row.IsActual9Null, 0, row.Actual9) + If(row.IsActual10Null, 0, row.Actual10) + If(row.IsActual11Null, 0, row.Actual11) + If(row.IsActual12Null, 0, row.Actual12)
                            Else
                                amount = If(row.IsAmountNull, 0, row.Amount)
                            End If
                            Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
                            Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                            If row.IsCmmtDetail_IdNull Then
                                Dim newId As Integer = adap.InsertBudgetCopy(Val(planYearT), Val(planYearT), vertionT, If(row.IsAssetNoNull, Nothing, row.AssetNo), If(row.IsAssetDescriptionNull, Nothing, row.AssetDescription), If(row.IsAssetRowNoNull, Nothing, row.AssetRowNo), If(row.IsOldAssetNoNull, Nothing, row.OldAssetNo), If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), If(row.IsPricePerUnitNull, Nothing, row.PricePerUnit), If(row.IsNoteNull, Nothing, row.Note), If(row.IsIdentificationNull, Nothing, row.Identification), If(row.IsFormIdNull, Nothing, row.FormId), If(row.IsFndPrgGenFlagNull, Nothing, row.FndPrgGenFlag), If(row.IsFundedProgramNull, Nothing, row.FundedProgram), If(row.IsTmpFundedProgramNull, Nothing, row.TmpFundedProgram), If(row.IsFunctionalAreaNull, Nothing, row.FunctionalArea), If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), If(row.IsAttachments_amountNull, Nothing, row.Attachments_amount), If(row.IsProductItemNoNull, Nothing, row.ProductItemNo), Date.Now, Nothing, user, Nothing, planYearT, If(row.IsActivityType_ActivityTypeCodeNull, Nothing, row.ActivityType_ActivityTypeCode), If(row.IsActivity_ActivityCodeNull, Nothing, row.Activity_ActivityCode), planYearT, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), If(row.IsProduct_ProductCodeNull, Nothing, Val(planYearT)), funcenter, planYearT, If(row.IsProject_ProjectCodeNull, Nothing, row.Project_ProjectCode), planYearT, If(row.IsProject_FundCenterCodeNull, Nothing, row.Project_FundCenterCode), Nothing, If(row.IsFund_FundCodeNull, Nothing, row.Fund_FundCode), planYearT, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), If(row.IsCmmt_CmmtCodeNull, Nothing, Val(planYearT)))
                                adapAt.copyAttachment(newId, row.Id)
                                adapBud12.copyBudget12Advance(Val(planYearT), vertionT, newId, row.Id)
                            Else
                                Dim newId As Integer = adap.InsertBudgetCopy(Val(planYearT), Val(planYearT), vertionT, If(row.IsAssetNoNull, Nothing, row.AssetNo), If(row.IsAssetDescriptionNull, Nothing, row.AssetDescription), If(row.IsAssetRowNoNull, Nothing, row.AssetRowNo), If(row.IsOldAssetNoNull, Nothing, row.OldAssetNo), If(row.IsElement1Null, Nothing, row.Element1), If(row.IsElement2Null, Nothing, row.Element2), If(row.IsElement3Null, Nothing, row.Element3), If(row.IsElement4Null, Nothing, row.Element4), If(row.IsElement5Null, Nothing, row.Element5), If(row.IsElement6Null, Nothing, row.Element6), If(row.IsElement7Null, Nothing, row.Element7), If(row.IsElement8Null, Nothing, row.Element8), If(row.IsElement9Null, Nothing, row.Element9), amount, If(row.IsNewQuantityNull, Nothing, row.NewQuantity), If(row.IsReplaceQuantityNull, Nothing, row.ReplaceQuantity), If(row.IsPricePerUnitNull, Nothing, row.PricePerUnit), If(row.IsNoteNull, Nothing, row.Note), If(row.IsIdentificationNull, Nothing, row.Identification), If(row.IsFormIdNull, Nothing, row.FormId), If(row.IsFndPrgGenFlagNull, Nothing, row.FndPrgGenFlag), If(row.IsFundedProgramNull, Nothing, row.FundedProgram), If(row.IsTmpFundedProgramNull, Nothing, row.TmpFundedProgram), If(row.IsFunctionalAreaNull, Nothing, row.FunctionalArea), If(row.IsActual1Null, Nothing, row.Actual1), If(row.IsActual2Null, Nothing, row.Actual2), If(row.IsActual3Null, Nothing, row.Actual3), If(row.IsActual4Null, Nothing, row.Actual4), If(row.IsActual5Null, Nothing, row.Actual5), If(row.IsActual6Null, Nothing, row.Actual6), If(row.IsActual7Null, Nothing, row.Actual7), If(row.IsActual8Null, Nothing, row.Actual8), If(row.IsActual9Null, Nothing, row.Actual9), If(row.IsActual10Null, Nothing, row.Actual10), If(row.IsActual11Null, Nothing, row.Actual11), If(row.IsActual12Null, Nothing, row.Actual12), If(row.IsActual13Null, Nothing, row.Actual13), If(row.IsActual14Null, Nothing, row.Actual14), If(row.IsActual15Null, Nothing, row.Actual15), If(row.IsActual16Null, Nothing, row.Actual16), If(row.IsAttachments_amountNull, Nothing, row.Attachments_amount), If(row.IsProductItemNoNull, Nothing, row.ProductItemNo), Date.Now, Nothing, user, Nothing, planYearT, If(row.IsActivityType_ActivityTypeCodeNull, Nothing, row.ActivityType_ActivityTypeCode), If(row.IsActivity_ActivityCodeNull, Nothing, row.Activity_ActivityCode), planYearT, If(row.IsProduct_ProductCodeNull, Nothing, row.Product_ProductCode), If(row.IsProduct_ProductCodeNull, Nothing, Val(planYearT)), funcenter, planYearT, If(row.IsProject_ProjectCodeNull, Nothing, row.Project_ProjectCode), planYearT, If(row.IsProject_FundCenterCodeNull, Nothing, row.Project_FundCenterCode), row.CmmtDetail_Id, If(row.IsFund_FundCodeNull, Nothing, row.Fund_FundCode), planYearT, If(row.IsCmmt_CmmtCodeNull, Nothing, row.Cmmt_CmmtCode), If(row.IsCmmt_CmmtCodeNull, Nothing, Val(planYearT)))
                                adapAt.copyAttachment(newId, row.Id)
                                adapBud12.copyBudget12Advance(Val(planYearT), vertionT, newId, row.Id)
                            End If
                        Next
                        adapRev.CoptRevPlanAdv2(Val(planYearT), vertionT, Val(planYearS), vertionS)
                    End If
                End If
                scope.Complete()
            Catch ex As Exception
                Transaction.Current.Rollback()
                Throw New Exception(ex.Message)
            End Try
        End Using
    End Sub
    Function getFund(ByVal groupP As String, ByVal planyear As Integer) As String
        Dim ogLevel As Integer
        Dim funcenter As String = ""
        Dim adap As New copyverAdvanceDSTableAdapters.fundpathTableAdapter
        For Each row As copyverAdvanceDS.fundpathRow In adap.GetOrgLevel(groupP, planyear)
            ogLevel = row.OrgLevel
            funcenter = row.FundCenter_FundCenterCode
        Next
        If ogLevel >= 30 Then
            Return funcenter
        End If
        Dim newGroup As String
        Dim len As Integer
        len = groupP.Length
        newGroup = groupP.Substring(0, len - 2)
        Return getFund(newGroup, planyear)
    End Function
End Class
